<?php
namespace app\sw\model;

use think\admin\model\SystemFile;
use think\Model;

class SheetModel extends Model{

    protected $name = 'sw_sheet';
    protected $pk = 'id';

    public $append = ['upload_time_str', 'file_url'];
    public function template(){
        return $this->hasOne(SheetTemplateModel::class, 'id', 'template_id');
    }


    public function getFileUrlAttr(){
        // return SITE_URL . $value;

        return filePathToUrl($this->file_path);
    }

    public function getUploadTimeStrAttr(){
        // return date('Y-m-d H:i:s', $value);
        // 通过最后一次登录时间，格式化时间戳。转化为 刚刚、几分钟前、几小时前、几天前、几月前、几年前
        if($this->upload_time == 0){
            return "-";
        }
        $time = time() - $this->upload_time;
        if($time < 60){
            return '刚刚';
        }else if($time < 3600){
            return floor($time / 60) . '分钟前';
        }else if($time < 86400){
            return floor($time / 3600) . '小时前';
        }else if($time < 2592000){
            return floor($time / 86400) . '天前';
        }else{
            return date('Y-m-d', $this->upload_time);
        }
    }

    
    public function department(){
        return $this->hasOne(DepartmentModel::class, 'id', 'dept_id');
    }




    public function resultFile(){
        return $this->hasOne(SystemFile::class, 'id', 'result_file_id')->field('id, name, xurl, xkey');
    }
    

    public function datas(){
        return $this->hasMany(SheetDataModel::class, 'sheet_id', 'id');
    }

}